package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.dao.UserMapper;
import com.example.admin.service.UserService;
import com.usian.model.admin.dtos.AdUserDto;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.utils.common.AppJwtUtil;
import com.usian.utils.common.MD5Utils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(AdUser adUser) {
        userMapper.insert(adUser);

    }

    @Override
    public List<AdUser> getUserList() {
        return userMapper.selectList(null);
    }

    @Override
    public List<AdUser> getUserList2() {
        Page<AdUser> page = new Page<AdUser>(1, 2);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", "zhaoyu");
        IPage<AdUser> adUserIPage = userMapper.selectPage(page, queryWrapper);
        List<AdUser> records = adUserIPage.getRecords();

        return records;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult login(AdUserDto adUserDto) {
        log.info("=========>adUserDto:{}",adUserDto);
        //参数校验
        if (adUserDto.getName() == null || adUserDto.getName().equals("")) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_NAME_REQUIRE);
        }
        if (adUserDto.getPassword() == null || adUserDto.getPassword().equals("")) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_PASSWORD_REQUIRE);
        }
        //根据用户名和密码查询
        LambdaQueryWrapper<AdUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //将前台的密码进行加密
        //加密方式是明文密码 进行加密
        String password = MD5Utils.encode(adUserDto.getPassword());
        lambdaQueryWrapper.eq(AdUser::getName, adUserDto.getName()).eq(AdUser::getPassword,password);
        List<AdUser> adUsers = userMapper.selectList(lambdaQueryWrapper);
        //判断非空
        if (CollectionUtils.isEmpty(adUsers)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_NAME_PASSWORD_ERROR);
        }
        //判断是否大于1个，超过一个说明数据库中保存的数据有问题
        if (adUsers.size() > 1) {
            log.info("根据用户名和密码查询到的用户信息列表，adUsers:{}",adUsers);
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //获取用户信息
        AdUser adUser = adUsers.get(0);
        log.info("根据用户名和密码查询到的用户信息adUser:{}",adUser);
        //根据需求构建map
        Map<String,Object> map  = new HashMap<>();
        map.put("id",adUser.getId());
        map.put("name",adUser.getName());
        map.put("nickName",adUser.getNickname());
        //生成token
        String token = AppJwtUtil.getToken(adUser.getId().longValue());
        map.put("token",token);
        return ResponseResult.okResult(map);

    }

}












